/*
 * VolumeUnit.cc
 *
 *  Created on: Sep 14, 2011
 *      Author: topcoder
 */
#include "VolumeUnit.h"
#include <sstream>
using namespace std;

VolumeUnit::VolumeUnit(int factor)
:factor(factor)
{
}

int VolumeUnit::getFactor() const
{
	return factor;
}

string VolumeUnit::formatToString(int amount)
{
	int oz = 0, tsp = 0, tbsp = 0;
	oz = amount/(4*3);
	tsp = (amount - 4*3*oz)/4;
	tbsp = amount - 4*3*oz -4* tsp;

	stringstream out;
	if(0 != oz)
		out<<oz<<" Oz";
	if(0 != tsp)
	{
		if(0 != oz)
		{
			out<<" and ";
		}
		out<<tsp<<" Tsp";
	}
	if(0 != tbsp)
	{
		if(0 != oz || 0 != tsp)
		{
			out<<" and ";
		}
		out<<tbsp<<" Tbsp";
	}

	if(!(oz||tsp||tbsp))
	{
		out<<"0 Tbsp";
	}

	return out.str();
}

string VolumeUnit::formatToStringInBaseUnit(int amount)
{
    stringstream out;
    out<<amount<<" Tbsp";
    return out.str();
}

VolumeUnit& VolumeUnit::OZ()
{
	static VolumeUnit oz(4*3);
	return oz;
}

VolumeUnit& VolumeUnit::TSP()
{
	static VolumeUnit tsp(4);
	return tsp;
}

VolumeUnit& VolumeUnit::TBSP()
{
	static VolumeUnit tbsp(1);
	return tbsp;
} 
